﻿using System;
using System.Windows.Forms;
using Glossary;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace TeacherSolution
{
    public partial class TeacherGUI : Form
    {
        private BindingSource bindingSource = new BindingSource();
        private SqlDataAdapter dataAdapter = new SqlDataAdapter();

        private void ClearAddWordObjects()
        {
            ForeignTextBox.Text = "";
            SwedishTextBox.Text = "";
            EnglishAddRadioButton.Checked = false;
            GermanAddRadioButton.Checked = false;
            SpanishAddRadioButton.Checked = false;
            AddWordButton.Enabled = false;
            caseSensitiveNoRadioButton.Checked = false;
            caseSensitiveYesRadioButton.Checked = false;
            correctExamButton.Enabled = false;
            ForeignLabel.Text = "Utländska";
            ExpTextBox.Text = "";
        }

        private void ClearCreateExamObjects()
        {
            englishRadioButton.Checked = false;
            spanishRadioButton.Checked = false;
            germanRadioButton.Checked = false;
            CreateExamButton.Enabled = false;
            ExamSizeNumericUpDown.Minimum = 10;
            caseSensitiveNoRadioButton.Checked = false;
            caseSensitiveYesRadioButton.Checked = false;
            correctExamButton.Enabled = false;
        }
        public TeacherGUI()
        {
            this.Load += new System.EventHandler(UncorrectedExam_Load);
            InitializeComponent();

        }

        private void UncorrectedExam_Load(object sender, EventArgs e)
        {
            dataGridView.DataSource = bindingSource;
            LoadGridView();
        }

        public void GetUncorrectedExams(string sqlQuery)
        {
            try
            {
                string connectionString = ConfigurationManager.ConnectionStrings["projectConnection"].ConnectionString;
                dataAdapter = new SqlDataAdapter(sqlQuery, connectionString);
                DataTable table = new DataTable();
                dataAdapter.Fill(table);
                bindingSource.DataSource = table;
            }
            catch (Exception)
            {

                MessageBox.Show("error");
            }
        }

        public void LoadGridView()
        {
            string cmd = "SELECT * FROM AllUncorrectedExamsView";
            GetUncorrectedExams(cmd);

        }

        private void GermanRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            CreateExamButton.Enabled = true;
        }

        private void EnglishRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            CreateExamButton.Enabled = true;
        }

        private void SpanishRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            CreateExamButton.Enabled = true;
        }

        private void CreateExamButton_Click(object sender, EventArgs e)
        {
            //Languages langSelected = 0;
            //if (englishRadioButton.Checked == true)
            //{
            //    langSelected = (Languages)1;
            //}
            //else if (germanRadioButton.Checked == true)
            //{
            //    langSelected = (Languages)2;
            //}
            //else if (spanishRadioButton.Checked == true)
            //{
            //    langSelected = (Languages)3;
            //}
            //Numbers examSize = (Numbers)ExamSizeNumericUpDown.Value;

            //try
            //{
            //    Glossary.Input.CreateExamFile(langSelected, examSize);
            //    statusLabel.Text = "Skapa prov gick felfritt.";
            //}
            //catch (DirectoryNotFoundException)
            //{
            //    statusLabel.Text = "En/Flera kataloger saknas och provet skapades ej.";
            //}
            //catch(Exception)
            //{
            //    statusLabel.Text = "Okänt fel uppstod och provet skapades ej.";
            //}
            //ClearCreateExamObjects();
            int courseId = 1;
            string todayDate = DateTime.Now.ToString("yyyy/MM/dd");
            if (englishRadioButton.Checked == true)
            {
                courseId = 1;
            }
            else if (germanRadioButton.Checked == true)
            {
                courseId = 2;
            }
            else if (spanishRadioButton.Checked == true)
            {
                courseId = 3;
            }
            int examSize = (int)ExamSizeNumericUpDown.Value;

            using (SqlConnection conn2 = new SqlConnection())
            {
                conn2.ConnectionString = ConfigurationManager.ConnectionStrings["projectConnection"].ConnectionString;
                SqlCommand cmd2 = new SqlCommand("AddNewExamProc", conn2);
                cmd2.CommandType = CommandType.StoredProcedure;

                cmd2.Parameters.AddWithValue("@CourseID", courseId);
                cmd2.Parameters.AddWithValue("@DateCreated", todayDate);
                cmd2.Parameters.AddWithValue("@amountofwords", examSize);

                conn2.Open();
                cmd2.ExecuteNonQuery();
                ClearCreateExamObjects();
                conn2.Close();
            }

        }

        private void EnglishAddRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            AddWordButton.Enabled = true;
            ForeignLabel.Text = "Engelska";
        }

        private void GermanAddRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            AddWordButton.Enabled = true;
            ForeignLabel.Text = "Tyska";
        }

        private void SpanishAddRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            AddWordButton.Enabled = true;
            ForeignLabel.Text = "Spanska";
        }

        private void AddWordButton_Click(object sender, EventArgs e)
        {
            string language = "";
            //Languages langSelected;
            if (EnglishAddRadioButton.Checked == true)
            {
                language = "English";
                //langSelected = (Languages)1;
            }
            else if (GermanAddRadioButton.Checked == true)
            {
                language = "German";
                //langSelected = (Languages)2;
            }
            else
            {
                language = "Spanish";
                //langSelected = (Languages)3;
            }

            string foreign = ForeignTextBox.Text;
            string swedish = SwedishTextBox.Text;
            string explanation = ExpTextBox.Text;

            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["projectConnection"].ConnectionString;
                SqlCommand cmd = new SqlCommand("AddNewWordProc", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@ForeignWord", foreign);
                cmd.Parameters.AddWithValue("@Language", language);
                cmd.Parameters.AddWithValue("@SwedishWord", swedish);
                cmd.Parameters.AddWithValue("@Explanation", explanation);

                conn.Open();
                cmd.ExecuteNonQuery();
                ClearAddWordObjects();
                conn.Close();
            }
            //if (foreign != "" || swedish != "")
            //{
            //    Glossary.Input.AddNewWord(foreign, swedish, langSelected);
            //    ClearAddWordObjects();
            //    statusLabel.Text = "Lägga till ord gick felfritt.";
            //}
            //else
            //    statusLabel.Text = "Det går inte att lägga in en tom glosa.";

        }

        private void CorrectExamButton_Click(object sender, EventArgs e)
        {

            
            //CaseSensitive cs;
            //if (caseSensitiveYesRadioButton.Checked == true)
            //{
            //    cs = CaseSensitive.CaseSensitive;
            //}
            //else
            //{
            //    cs = CaseSensitive.NonCaseSensitive;
            //}

            //OpenFileDialog fileDialog = new OpenFileDialog();
            //fileDialog.InitialDirectory = Directory.GetCurrentDirectory() + "\\UncorrectedExams\\";
            //fileDialog.ShowDialog();
            //string selectedPath = fileDialog.FileName;
            //try
            //{
            //    Input.CorrectExam(selectedPath, cs);
            //    caseSensitiveNoRadioButton.Checked = false;
            //    caseSensitiveYesRadioButton.Checked = false;
            //    correctExamButton.Enabled = false;
            //    statusLabel.Text = "Provet blev rättat.";

            //}
            //catch (FileNotFoundException)
            //{
            //    statusLabel.Text = "Du har inte angett en fil. Försök igen.";
            //}
            //catch (DirectoryNotFoundException)
            //{
            //    statusLabel.Text = "Katalogen finns ej och provet kunde ej rättas.";
            //}
        }

        private void CaseSensitiveYesRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            correctExamButton.Enabled = true;
        }

        private void CaseSensitiveNoRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            correctExamButton.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            correctExamButton.Enabled = true;

        }
    }
}
